<html>
<head>
  <title>利用数组进行数据处理</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="549"/>
<h1>利用数组进行数据处理</h1>

<div>
<span><div>• NumPy数组使你可以将许多种数据处理任务表述为简洁的数组表达式（否则需</div><div>要编写循环）。用数组表达式代替循环的做法，通常被称为矢量化。</div><div>• 矢量化数组运算要比等价的纯Python方式快上一两个数量级</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import matplotlib.pyplot as plt</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy as np</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import pylab</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">points = np.arange(-5, 5, 0.01) # 生成100个点</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">xs, ys = np.meshgrid(points, points)  # xs, ys互为转置矩阵</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print xs</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print ys</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">z = np.sqrt(xs ** 2 + ys ** 2)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print z</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># 画图</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">plt.imshow(z, cmap = plt.cm.gray);</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">plt.colorbar()</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">plt.title(&quot;Image plot of $\sqrt{x^2 + y^2}$ for a grid of values&quot;)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">pylab.show()</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 16pt;"><span style="color: rgb(28, 51, 135); font-size: 16pt; font-weight: bold;">利用数组进行数据处理    将条件逻辑表述为数组运算</span></font></div><div><br/></div><div>• 列表推导的局限性</div><div>• 纯Python代码，速度不够快。</div><div>• 无法应用于高维数组</div><div>• where和where的嵌套</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">import numpy as np</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">import numpy.random as np_random</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">'''</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">关于zip函数的一点解释，zip可以接受任意多参数，然后重新组合成1个tuple列表。</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">zip([1, 2, 3], [4, 5, 6], [7, 8, 9])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">返回结果：[(1, 4, 7), (2, 5, 8), (3, 6, 9)]</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">'''</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 9pt;">print('</span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">通过真值表选择元素</span><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 9pt;">')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">x_arr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">y_arr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">cond = np.array([True, False, True, True, False])</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">result = [( x if c else y) for x, y, c in zip(x_arr, y_arr, cond)] </span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># 通过列表推到实现</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(result)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(np.where(cond, x_arr, y_arr)) </span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;"># 使用NumPy的where函数</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print()</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 9pt;">print('</span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">更多where的例子</span><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 9pt;">')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arr = np_random.randn(4, 4)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(arr)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(np.where(arr &gt; 0, 2, -2))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(np.where(arr &gt; 0, 2, arr))</span></div></div><div><font style="font-size: 12pt;"><span style="color: rgb(227, 0, 0); font-size: 12pt; font-weight: bold;">where 相当于一个if else 语句：针对ndrray对象的每个单个元素</span></font></div><div><img src="利用数组进行数据处理_files/Image.png" type="image/png" data-filename="Image.png"/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print('where嵌套')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">cond_1 = np.array([True, False, True, True, False])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">cond_2 = np.array([False, True, False, True, False])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># 传统代码如下</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">result = [ </span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;">]</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;">for i in xrange(len(cond)):</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;">    if cond_1[i] and cond_2[i]:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;">        result.append(0)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;">    elif cond_1[i]:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;">        result.append(1)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;">    elif cond_2[i]:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;">        result.append(2)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;">    else:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;">        result.append(3)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; line-height: 1.45;">print (result)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># np版本代码</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">result = np.where(cond_1 &amp; cond_2, 0, np.where(cond_1, 1, np.where(cond_2, 2, 3))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(result)</span></div></div><div><img src="利用数组进行数据处理_files/Image [1].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">数学和统计方法</span></font></div><div>• 数学和统计方法</div><div><img src="利用数组进行数据处理_files/Image [2].png" type="image/png" data-filename="Image.png"/></div><div>• 标准差和方差的解释</div><div>• cumsum和cumprod的解释</div><div>• 带axis参数的统计函数</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">import numpy as np</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">import numpy.random as np_random</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print('</span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">求和，求平均</span><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">')</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">arr = np.random.randn(5, 4)</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print(arr)</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print(arr.mean())</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print(arr.sum())</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print(arr.mean(axis = 1))    </span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># 对每一行的元素求平均</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print(arr.sum(0))   </span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># 对每一列元素求和，axis可以省略。</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print()</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">'''</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">cumsum:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">- 按列操作：a[i][j] += a[i - 1][j]</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">- 按行操作：a[i][j] *= a[i][j - 1]</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">cumprod:</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">- 按列操作：a[i][j] += a[i - 1][j]</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">- 按行操作：a[i][j] *= a[i][j - 1]</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">'''</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print( 'cunsum和cumprod函数演示')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arr = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(arr.cumsum(0))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print(arr.cumprod(1))</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">用于布尔型数组的方法</span></font></div><div>• sum对True值计数</div><div>• any和all测试布尔型数组，对于非布尔型数组，所有非0元素将会被当做True。</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy as np</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy.random as np_random</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print '对正数求和'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arr = np_random.randn(100)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print ((arr &gt; 0).sum())</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print()</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print '对数组逻辑操作'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">bools = np.array([False, False, True, False])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print (bools.any())# 有一个为True则返回True</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print (bools.all())# 有一个为False则返回False</span></div></div><div><img src="利用数组进行数据处理_files/Image [3].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">排序</span></font></div><div>• 直接排序</div><div>• 指定轴排序</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy as np</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy.random as np_random</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 12px;">print('</span><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">一维数组排序</span><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 12px;">')</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 12px;">arr = np_random.randn(8)</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 12px;">arr.sort()</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 12px;">print(arr)</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco; font-size: 12px;">print()</span></div><div><br/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print ('二维数组排序')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arr = np_random.randn(5, 3)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print (arr)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arr.sort(1) # 对每一行元素做排序</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print (arr)</span></div><div><br/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print( '找位置在5%的数字')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">large_arr = np_random.randn(1000)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">large_arr.sort()</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print (large_arr[int(0.05 * len(large_arr))])</span></div></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">去重以及其它集合运算</span></font></div><div>• 去重以及其它集合运算</div><div><img src="利用数组进行数据处理_files/Image [4].png" type="image/png" data-filename="Image.png"/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy as np</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy.random as np_random</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print '用unique函数去重'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print sorted(set(names))  # 传统Python做法</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print np.unique(names)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print np.unique(ints)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print '查找数组元素是否在另一数组'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">values = np.array([6, 0, 0, 3, 2, 5, 6])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print np.in1d(values, [2, 3, 6])</span></div></div><div><img src="利用数组进行数据处理_files/Image [5].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div style="text-align: center;"><span style="min-height: 41pt; font-size: 32pt;"><span style="font-family: MicrosoftYaHei; font-size: 32pt; font-weight: bold; color: rgb(28, 51, 135);">数组文件的输入输出</span></span></div><div>• 将数组以二进制格式保存到磁盘</div><div>• 存取文本文件</div><div><font style="font-size: 12pt;"><span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">读取：</span></font></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy as np</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print （'读取csv文件做为数组'）</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arr = np.loadtxt('array_ex.txt', delimiter = ',')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print （arr）</span></div></div><div><br/></div><div><font style="font-size: 12pt;"><span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">写入：</span></font></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy as np</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print '数组文件读写'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arr = np.arange(10)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">np.save('some_array', arr)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print np.load('some_array.npy')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print '多个数组压缩存储'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">np.savez('array_archive.npz', a = arr, b = arr)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arch = np.load('array_archive.npz')</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print arch['b']</span></div></div><div><img src="利用数组进行数据处理_files/Image [6].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">线性代数</span></font></div><div>•常用的numpy.linalg函数 </div><div><img src="利用数组进行数据处理_files/Image [7].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><img src="利用数组进行数据处理_files/Image [8].png" type="image/png" data-filename="Image.png"/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy as np</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy.random as np_random</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">from numpy.linalg import inv, qr</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print '矩阵乘法'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">x = np.array([[1., 2., 3.], [4., 5., 6.]])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">y = np.array([[6., 23.], [-1, 7], [8, 9]])</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print x.dot(y)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print np.dot(x, np.ones(3))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">x = np_random.randn(5, 5)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print '矩阵求逆'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">mat = x.T.dot(x)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print inv(mat)  # 矩阵求逆</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print mat.dot(inv(mat)) # 与逆矩阵相乘，得到单位矩阵。</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print '矩阵消元'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print mat</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">q, r = qr(mat)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print q</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print r</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># TODO:</span> <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.qr.html" style="font-size: 9pt; background-color: rgb(251, 250, 248); font-family: Monaco; color: rgb(51, 51, 51);">http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.qr.html</a> <span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">q代表什么矩阵？</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">随机数生成</span></font></div><div>• 部分numpy.random函数 </div><div><img src="利用数组进行数据处理_files/Image [9].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><img src="利用数组进行数据处理_files/Image [10].png" type="image/png" data-filename="Image.png"/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy as np</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy.random as np_random</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">from random import normalvariate</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print '正态分布随机数'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">samples = np.random.normal(size=(4, 4))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print samples</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print '批量按正态分布生成0到1的随机数'</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">N = 10</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print [normalvariate(0, 1) for _ in xrange(N)]</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print np.random.normal(size = N)  # 与上面代码等价</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">高级应用 数组重塑</span></font></div><div>• reshape重塑数组</div><div>• -1自动推导维度大小</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">import numpy as np</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print &quot;将一维数组转换为二维数组&quot;</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arr = np.arange(8)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print arr.reshape((4, 2))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print arr.reshape((4, 2)).reshape((2, 4)) # 支持链式操作</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print &quot;维度大小自动推导&quot;</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arr = np.arange(15)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print arr.reshape((5, -1))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print &quot;获取维度信息并应用&quot;</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">other_arr = np.ones((3, 5))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print other_arr.shape</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print arr.reshape(other_arr.shape)</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print &quot;高维数组拉平&quot;</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">arr = np.arange(15).reshape((5, 3))</span></div><div><span style="font-size: 9pt; background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco;">print arr.ravel()</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><font style="font-size: 18pt;"><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: bold;">数组的合并和拆分</span></font></div><div>• 数组连接函数</div><div><img src="利用数组进行数据处理_files/Image [11].png" type="image/png" data-filename="Image.png"/></div><div><span>•</span> <span>_r对象</span></div><div><span>•</span> <span>_c对象</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div># -*- coding: utf-8 -*-</div><div><br/></div><div>import numpy as np</div><div>import numpy.random as np_random</div><div><br/></div><div>print('<span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">连接两个二维数组</span>')</div><div>arr1 = np.array([[1, 2, 3], [4, 5, 6]])</div><div>arr2 = np.array([[7, 8, 9], [10, 11, 12]])</div><div>print (np.concatenate([arr1, arr2], axis = 0)) <span style="font-family: Monaco; font-size: 9pt; line-height: 1.45;">  # 按行连接</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (np.concatenate([arr1, arr2], axis = 1))  # 按列连接</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print()</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><br/></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># 所谓堆叠，参考叠盘子。。。连接的另一种表述</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print ('垂直stack与水平stack')</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (np.vstack((arr1, arr2))) # 垂直堆叠</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (np.hstack((arr1, arr2))) # 水平堆叠</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print()</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><br/></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print ('拆分数组')</span></div><div><font face="Monaco">arr = np_random.randn(5, 5)</font></div><div><font face="Monaco">print(arr)</font></div><div><font face="Monaco">print('水平拆分')</font></div><div><font face="Monaco">first, second, third = np.split(arr, [1, 3], axis = 0)</font></div><div><font style="font-family: Monaco;">print('first')</font></div><div><font face="Monaco">print(first)</font></div><div><font face="Monaco">print('second')</font></div><div><font face="Monaco">print(second)</font></div><div><font face="Monaco">print('third')</font></div><div><font face="Monaco">print(third)</font></div><div><font face="Monaco">print('垂直拆分')</font></div><div><font face="Monaco">first, second, third = np.split(arr, [1, 3], axis = 1)</font></div><div><span style="font-family: Monaco;">print('first')</span></div><div><span style="font-family: Monaco;">print(first)</span></div><div><span style="font-family: Monaco;">print('second')</span></div><div><span style="font-family: Monaco;">print(second)</span></div><div><span style="font-family: Monaco;">print('third')</span></div><div><span style="font-family: Monaco;">print(third)</span></div><div><span style="font-family: Monaco;">print()</span></div><div><span style="font-family: Monaco;"><br/></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># 堆叠辅助类</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np.range(6)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr1 = arr.reshape((3,2))</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr2 = arr_random.randn(3,2)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print ('r_用于按行堆叠')</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (np.r_[arr1, arr2])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print ('c_用于按列堆叠')</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (np.c_[np.r_[arr1, arr2], arr])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print ('切片直接转为数组')</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print (np.c_[1:6, -10:-5])</span></div></div><div><img src="利用数组进行数据处理_files/Image [12].png" type="image/png" data-filename="Image.png" width="232"/><img src="利用数组进行数据处理_files/Image [13].png" type="image/png" data-filename="Image.png" width="428"/></div><div><img src="利用数组进行数据处理_files/Image [14].png" type="image/png" data-filename="Image.png" width="229"/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><span><font style="font-size: 18pt; color: rgb(28, 51, 135);"><b>元素的重复操作</b></font></span></div><div><span>•</span> <span>_tile</span></div><div><span>•</span> <span>_repeat</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">import numpy as np</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">import numpy.random as np_random</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print 'Repeat: 按元素'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np.arange(3)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr.repeat(3)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr.repeat([2, 3, 4]) # 3个元素，分别复制2, 3, 4次。长度要匹配！</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print 'Repeat，指定轴'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np_random.randn(2, 2)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr.repeat(2, axis = 0) # 按行repeat</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr.repeat(2, axis = 1) # 按列repeat</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr.repeat(2, axis = 0) # 按行repeat</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print 'Tile: 参考贴瓷砖'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print np.tile(arr, 2)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print np.tile(arr, (2, 3))  # 指定每个轴的tile次数</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><span><font style="font-size: 18pt; color: rgb(28, 51, 135);"><b>花式索引的等价函数</b></font></span></div><div><span>•</span> <span>take</span></div><div><span>•</span> <span>put</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># -*- coding: utf-8 -*-</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">import numpy as np</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">import numpy.random as np_random</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print 'Fancy Indexing例子代码'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np.arange(10) * 100</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">inds = [7, 1, 2, 6]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr[inds]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print '使用take'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr.take(inds)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print '使用put更新内容'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr.put(inds, 50)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr.put(inds, [70, 10, 20, 60])</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print 'take，指定轴'</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">arr = np_random.randn(2, 4)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">inds = [2, 0, 2, 1]</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">print arr.take(inds, axis = 1)  # 按列take</span></div><div><br style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"/></div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></span>
</div></body></html> 